package com.th.supcom.pds.impl.service.internal.phamapply;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.th.supcom.pds.domain.pojo.InpPhamApplyDetail;
import com.th.supcom.pds.domain.pojo.InpPhamApplyDetailSummary;
import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.pojo.PdsPatPhamRemain;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;

public final class PhamApplyConvertUtils
{
    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamApplyConvertUtils.class);

    private PhamApplyConvertUtils ()
    {

    }

    /**
     * 将药品请领明细按照请领单号分组
     * 
     * @param detailList 请领明细List
     * @param paivisitIdList 患者住院流水号List
     * @return
     */
    public static List <List <InpPhamApplyDetailVO>> groupDeatilListByApplyNo (List <InpPhamApplyDetailVO> detailList)
    {
        List <List <InpPhamApplyDetailVO>> result = new ArrayList <List <InpPhamApplyDetailVO>> ();
        Map <String, List <InpPhamApplyDetailVO>> map = new HashMap <String, List <InpPhamApplyDetailVO>> ();
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            List <InpPhamApplyDetailVO> temp = null;

            if (map.containsKey (inpPhamApplyDetailVO.getApplyNo ()))
            {
                temp = map.get (inpPhamApplyDetailVO.getApplyNo ());
            }
            else
            {
                temp = new ArrayList <InpPhamApplyDetailVO> ();
            }
            temp.add (inpPhamApplyDetailVO);
            map.put (inpPhamApplyDetailVO.getApplyNo (), temp);
        }
        Set <Map.Entry <String, List <InpPhamApplyDetailVO>>> set = map.entrySet ();
        for (Iterator <Map.Entry <String, List <InpPhamApplyDetailVO>>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <String, List <InpPhamApplyDetailVO>> entry = (Map.Entry <String, List <InpPhamApplyDetailVO>>) it.next ();
            result.add (entry.getValue ());
        }
        return result;
    }

    /**
     * 将药品请领明细按照患者分组
     * 
     * @param detailList 请领明细List
     * @param paivisitIdList 患者住院流水号List
     * @return
     */
    public static Map <Long, List <InpPhamApplyDetailVO>> convertListToMapByPaivisitID (List <InpPhamApplyDetailVO> detailList, List <Long> paivisitIdList)
    {
        Map <Long, List <InpPhamApplyDetailVO>> map = new HashMap <Long, List <InpPhamApplyDetailVO>> ();
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            List <InpPhamApplyDetailVO> temp = null;

            if (map.containsKey (inpPhamApplyDetailVO.getPaiVisitId ()))
            {
                temp = map.get (inpPhamApplyDetailVO.getPaiVisitId ());
            }
            else
            {
                paivisitIdList.add (inpPhamApplyDetailVO.getPaiVisitId ());
                temp = new ArrayList <InpPhamApplyDetailVO> ();
            }
            temp.add (inpPhamApplyDetailVO);
            map.put (inpPhamApplyDetailVO.getPaiVisitId (), temp);
        }
        return map;
    }

    /**
     * 将药品请领明细按照患者分组
     * 
     * @param detailList 请领明细List
     * @param paivisitIdList 患者住院流水号List
     * @return
     */
    public static Map <Long, List <InpPhamApplyDetailVO>> convertListToMapByPaivisitID (List <InpPhamApplyDetailVO> detailList)
    {
        Map <Long, List <InpPhamApplyDetailVO>> map = new HashMap <Long, List <InpPhamApplyDetailVO>> ();
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            List <InpPhamApplyDetailVO> temp = null;

            if (map.containsKey (inpPhamApplyDetailVO.getPaiVisitId ()))
            {
                temp = map.get (inpPhamApplyDetailVO.getPaiVisitId ());
            }
            else
            {
                temp = new ArrayList <InpPhamApplyDetailVO> ();
            }
            temp.add (inpPhamApplyDetailVO);
            map.put (inpPhamApplyDetailVO.getPaiVisitId (), temp);
        }
        return map;
    }

    /**
     * 将药品请领明细按照执行科室分类
     * 
     * @param detailList
     * @return
     */
    public static Map <String, List <InpPhamApplyDetail>> convertListToMapByApplyDispensary (List <InpPhamApplyDetail> detailList, List <String> applyDispensaryList)
    {
        Map <String, List <InpPhamApplyDetail>> map = new HashMap <String, List <InpPhamApplyDetail>> ();
        for (InpPhamApplyDetail inpPhamApplyDetail : detailList)
        {
            List <InpPhamApplyDetail> temp = null;

            if (map.containsKey (inpPhamApplyDetail.getApplyDispensary ()))
            {
                temp = map.get (inpPhamApplyDetail.getApplyDispensary ());
            }
            else
            {
                applyDispensaryList.add (inpPhamApplyDetail.getApplyDispensary ());
                temp = new ArrayList <InpPhamApplyDetail> ();
            }
            temp.add (inpPhamApplyDetail);
            map.put (inpPhamApplyDetail.getApplyDispensary (), temp);
        }
        return map;
    }

    /**
     * 将按照患者分组的药品请领明细转换为List
     * 
     * @param detailMap
     * @return
     */
    public static List <InpPhamApplyDetailVO> convertListToMapByPaivisitID (Map <Long, List <InpPhamApplyDetailVO>> detailMap)
    {
        List <InpPhamApplyDetailVO> result = new ArrayList <InpPhamApplyDetailVO> ();
        // 遍历Map
        Set <Map.Entry <Long, List <InpPhamApplyDetailVO>>> set = detailMap.entrySet ();
        for (Iterator <Map.Entry <Long, List <InpPhamApplyDetailVO>>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <Long, List <InpPhamApplyDetailVO>> entry = (Map.Entry <Long, List <InpPhamApplyDetailVO>>) it.next ();
            result.addAll (entry.getValue ());
        }
        return result;
    }

    /**
     * 将药品请领明细按照药品标准码分类
     * 
     * @param detailList
     * @return
     */
    public static Map <String, List <InpPhamApplyDetailVO>> convertListToMapByPhamStdCode (List <InpPhamApplyDetailVO> detailList, List <String> phamStdCodeList)
    {
        Map <String, List <InpPhamApplyDetailVO>> map = new HashMap <String, List <InpPhamApplyDetailVO>> ();
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            List <InpPhamApplyDetailVO> temp = null;

            if (map.containsKey (inpPhamApplyDetailVO.getPhamStdCode ()))
            {
                temp = map.get (inpPhamApplyDetailVO.getPhamStdCode ());
            }
            else
            {
                phamStdCodeList.add (inpPhamApplyDetailVO.getPhamStdCode ());
                temp = new ArrayList <InpPhamApplyDetailVO> ();
            }
            temp.add (inpPhamApplyDetailVO);
            map.put (inpPhamApplyDetailVO.getPhamStdCode (), temp);
        }
        return map;
    }

    /**
     * 将药品请领明细按照药品标准码分类
     * 
     * @param detailList
     * @return
     */
    public static Map <String, List <InpPhamApplyDetailVO>> convertListToMapByPhamStdCode (List <InpPhamApplyDetailVO> detailList)
    {
        Map <String, List <InpPhamApplyDetailVO>> map = new HashMap <String, List <InpPhamApplyDetailVO>> ();
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            List <InpPhamApplyDetailVO> temp = null;

            if (map.containsKey (inpPhamApplyDetailVO.getPhamStdCode ()))
            {
                temp = map.get (inpPhamApplyDetailVO.getPhamStdCode ());
            }
            else
            {
                temp = new ArrayList <InpPhamApplyDetailVO> ();
            }
            temp.add (inpPhamApplyDetailVO);
            map.put (inpPhamApplyDetailVO.getPhamStdCode (), temp);
        }
        return map;
    }
    /**
     * 将已汇总的冲抵药品标准码转化为map
     * 
     * @param detailList
     * @return
     */
    public static Map <String, InpPhamApplyDetailVO> convertSummaryListToMapByPhamStdCode (List <InpPhamApplyDetailVO> detailList)
    {
        Map <String, InpPhamApplyDetailVO> map = new HashMap <String, InpPhamApplyDetailVO> ();
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            map.put (inpPhamApplyDetailVO.getPhamStdCode (), inpPhamApplyDetailVO);
        }
        return map;
    }

    /**
     * 将药品请领明细按照医嘱序号分类
     * 
     * @param detailList
     * @return
     */
    public static Map <Long, List <InpPhamApplyDetailVO>> convertListToMapByOrderNo (List <InpPhamApplyDetailVO> detailList)
    {
        Map <Long, List <InpPhamApplyDetailVO>> map = new HashMap <Long, List <InpPhamApplyDetailVO>> ();
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            List <InpPhamApplyDetailVO> temp = null;

            if (map.containsKey (inpPhamApplyDetailVO.getOrderNo ()))
            {
                temp = map.get (inpPhamApplyDetailVO.getOrderNo ());
            }
            else
            {
                temp = new ArrayList <InpPhamApplyDetailVO> ();
            }
            temp.add (inpPhamApplyDetailVO);
            map.put (inpPhamApplyDetailVO.getOrderNo (), temp);
        }
        return map;
    }

    /**
     * 将药品请领明细按照医嘱子序号分类
     * 
     * @param detailList
     * @return
     */
    public static Map <Long, List <InpPhamApplyDetailVO>> convertListToMapByOrderSubNo (List <InpPhamApplyDetailVO> detailList)
    {
        Map <Long, List <InpPhamApplyDetailVO>> map = new HashMap <Long, List <InpPhamApplyDetailVO>> ();
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : detailList)
        {
            List <InpPhamApplyDetailVO> temp = null;

            if (map.containsKey (inpPhamApplyDetailVO.getOrderSubNo ()))
            {
                temp = map.get (inpPhamApplyDetailVO.getOrderSubNo ());
            }
            else
            {
                temp = new ArrayList <InpPhamApplyDetailVO> ();
            }
            temp.add (inpPhamApplyDetailVO);
            map.put (inpPhamApplyDetailVO.getOrderSubNo (), temp);
        }
        return map;
    }

    /**
     * 将按照药品标准码分类的药品请领明细组合为List
     * 
     * @param detailMap
     * @return
     */
    public static List <InpPhamApplyDetailVO> convertMapToListByPhamStdCode (Map <String, List <InpPhamApplyDetailVO>> detailMap)
    {
        List <InpPhamApplyDetailVO> result = new ArrayList <InpPhamApplyDetailVO> ();
        // 遍历Map
        Set <Map.Entry <String, List <InpPhamApplyDetailVO>>> set = detailMap.entrySet ();
        for (Iterator <Map.Entry <String, List <InpPhamApplyDetailVO>>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <String, List <InpPhamApplyDetailVO>> entry = (Map.Entry <String, List <InpPhamApplyDetailVO>>) it.next ();
            result.addAll (entry.getValue ());
        }
        return result;
    }

    /**
     * 将药品属性信息按照药品标准码分类
     * 
     * @param summaryList
     * @return
     */
    public static Map <String, InpPhamApplyDetailSummary> convertPhamInfoListToMapByPhamStdCode (List <InpPhamApplyDetailSummary> summaryList)
    {
        Map <String, InpPhamApplyDetailSummary> map = new HashMap <String, InpPhamApplyDetailSummary> ();
        for (InpPhamApplyDetailSummary inpPhamApplyDetailSummary : summaryList)
        {
            map.put (inpPhamApplyDetailSummary.getPhamStdCode (), inpPhamApplyDetailSummary);
        }
        return map;
    }

    /**
     * 将按照药品标准码分类的药品属性信息组合为List
     * 
     * @param summaryMap
     * @return
     */
    public static List <InpPhamApplyDetailSummary> convertPhamInfoMapToListByPhamStdCode (Map <String, InpPhamApplyDetailSummary> summaryMap)
    {
        List <InpPhamApplyDetailSummary> result = new ArrayList <InpPhamApplyDetailSummary> ();
        // 遍历Map
        Set <Map.Entry <String, InpPhamApplyDetailSummary>> set = summaryMap.entrySet ();
        for (Iterator <Map.Entry <String, InpPhamApplyDetailSummary>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <String, InpPhamApplyDetailSummary> entry = (Map.Entry <String, InpPhamApplyDetailSummary>) it.next ();
            result.add (entry.getValue ());
        }
        return result;
    }

    /**
     * 将患者住院信息List转换成Map
     * 
     * @param patInfos
     * @return map, key=paiVisitId, value=list
     */
    public static Map <Long, InpPhamApplyPatInfo> convertPatInfoListToMapByPaivisitId (List <InpPhamApplyPatInfo> patInfos)
    {
        Map <Long, InpPhamApplyPatInfo> map = new HashMap <Long, InpPhamApplyPatInfo> ();

        for (InpPhamApplyPatInfo patInfo : patInfos)
        {
            map.put (patInfo.getPaiVisitId (), patInfo);
        }
        return map;
    }

    /**
     * 将患者药品剩余数量List转换成Map
     * 
     * @param lefts
     * @return map; key=paivisitId, value=list
     */
    public static Map <Long, List <PdsPatPhamRemain>> convertPhamLeftListToMapByPaivisitId (List <PdsPatPhamRemain> lefts)
    {
        Map <Long, List <PdsPatPhamRemain>> map = new HashMap <Long, List <PdsPatPhamRemain>> ();

        for (PdsPatPhamRemain left : lefts)
        {
            List <PdsPatPhamRemain> list = null;
            if (map.containsKey (left.getPaiVisitId ()))
            {
                list = map.get (left.getPaiVisitId ());

            }
            else
            {
                list = new ArrayList <PdsPatPhamRemain> ();
            }
            list.add (left);
            map.put (left.getPaiVisitId (), list);
        }
        return map;
    }

    /**
     * 根据药品编码将药品List转换成Map
     * 
     * @param lefts
     * @return
     */
    public static Map <String, PdsPatPhamRemain> convertPhamLeftListToMapByPhamStdCode (List <PdsPatPhamRemain> lefts)
    {

        Map <String, PdsPatPhamRemain> map = new HashMap <String, PdsPatPhamRemain> ();

        if (CollectionUtils.isEmpty (lefts))
        {
            return map;
        }

        for (PdsPatPhamRemain left : lefts)
        {
            map.put (left.getPhamStdCode (), left);
        }
        return map;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
